﻿//*****************************************************************************
//
//  Silver Games - Checkers
//  Copyright © 2009 Randi Relander
//      <rjrelander@users.sourceforge.net>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//*****************************************************************************

using System;
using System.Windows;
using System.Windows.Controls;

namespace Checkers.Client
{
    public partial class Tile : UserControl
    {
        public Tile(Board boardView, Checkers.Common.Tile tileModel)
        {
            InitializeComponent();

            mBoardView = boardView;

            mTileModel = tileModel;

            if (mTileModel.Color == Checkers.Common.TileColor.Red)
            {
                mRedBase.Visibility = Visibility.Visible;
            }
            else
            {
                mBlackBase.Visibility = Visibility.Visible;
            }

            MouseEnter += new System.Windows.Input.MouseEventHandler(Tile_MouseEnter);
            MouseLeave += new System.Windows.Input.MouseEventHandler(Tile_MouseLeave);

            MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(Tile_MouseLeftButtonDown);
        }

        public Point Center
        {
            get
            {
                double dx = 50.0 * Math.Sqrt(2.0) / 2.0;
                double dy = 50.0 * Math.Sqrt(2.0) / 4.0;

                double x = (double)X;
                double y = (double)Y;

                return new Point((x + y) * dx - 7.0 * dx, (x - y) * dy);
            }
        }

        void Tile_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            mBoardView.OnTileClicked(this);
        }

        public void Select1()
        {
            mSelectAnimation2.Stop();
            mSelectAnimation1.Begin();
        }

        public void Select2()
        {
            mSelectAnimation1.Stop();
            mSelectAnimation2.Begin();
        }

        public void Deselect()
        {
            mSelectAnimation1.Stop();
            mSelectAnimation2.Stop();
        }

        void Tile_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mHilight.Opacity = 1;
        }

        void Tile_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mHilight.Opacity = 0;
        }

        Board mBoardView;

        Checkers.Common.Tile mTileModel;

        public int X { get { return mTileModel.X; } }

        public int Y { get { return mTileModel.Y; } }

        public Checkers.Common.Tile Model { get { return mTileModel; } }
    }
}
